/*
 * test_controller.c
 *
 *  Created on: Jul 6, 2009
 *      Author: jacky
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "controllers/controller.h"


#include "test_controller.h"
#include "layout/layout.h"
#include "test/test_view.h"


DEFINE_ACTION_FUNC(index_action)
{
	return ActionOK;
}

DEFINE_ACTION_FUNC(upload_action)
{
	if (request->method == GET)
	{
		response->layout = default_layout;
		response->view = upload_view;
	}
	else
	{
		char buf[1024];
		int n;
		FILE *fp;
		if ((fp = fopen("/tmp/tmp.txt", "w")))
		{
			response->autoRender = 0; /* TODO:Jacky 关闭自动渲染 */
			while ((n = fread(buf, sizeof(buf), 1, stdin)))
			{
				fwrite(buf,sizeof(buf), n, fp);
			}
			fclose(fp);
		}
		else
		{
			response->status = 401;
			response->extra_header = "WWW-Authenticate: Basic realm=\"sss\"";
		}
	}
	return ActionOK;
}

DEFINE_ACTION_FUNC(info_action)
{
	response->layout = default_layout;
	response->view = info_view;
	return ActionOK;
}
#undef  CUR_CTL
#define CUR_CTL(...) test_##__VA_ARGS__
DEFINE_ACTIONS(
INIT_ACTION(index,index_action),
INIT_ACTION(info,info_action),
INIT_ACTION(upload,upload_action),
);
